package cn.wyl.order.service.impl;

import cn.wyl.basic.util.AjaxResult;
import cn.wyl.basic.util.CodeGenerateUtils;
import cn.wyl.basic.util.LoginContext;
import cn.wyl.order.domain.OrderProduct;
import cn.wyl.order.dto.OrderDto;
import cn.wyl.order.mapper.OrderProductMapper;
import cn.wyl.order.service.IOrderProductService;
import cn.wyl.basic.service.impl.BaseServiceImpl;
import cn.wyl.user.domain.Logininfo;
import cn.wyl.user.domain.User;
import cn.wyl.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Map;

/**
 * 业务实现类：
 */
@Service
public class OrderProductServiceImpl extends BaseServiceImpl<OrderProduct> implements IOrderProductService {

    @Autowired
    private OrderProductMapper orderProductMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public AjaxResult order(OrderDto orderDto, HttpServletRequest request) {
        Logininfo logininfo = LoginContext.getLogininfo(request);
        User user = userMapper.findByLogininfoId(logininfo.getId());

        OrderProduct orderProduct = new OrderProduct();
        orderProduct.setUserId(user.getId());
        orderProduct.setState(1);
        orderProduct.setDigest(orderDto.getName());
        orderProduct.setPrice(orderDto.getSaleprice());
        orderProduct.setOrderSn(CodeGenerateUtils.generateOrderSn(user.getId()));

        orderProductMapper.save(orderProduct);

        return new AjaxResult();
    }
}
